﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

[RequireComponent(typeof(FingerDownDetector))]
[RequireComponent(typeof(FingerMotionDetector))]
[RequireComponent(typeof(FingerHoverDetector))]
[RequireComponent(typeof(FingerUpDetector))]
[RequireComponent(typeof(TapRecognizer))]
[RequireComponent(typeof(LongPressRecognizer))]
[RequireComponent(typeof(DragRecognizer))]
[RequireComponent(typeof(PinchRecognizer))]
[RequireComponent(typeof(SwipeRecognizer))]
[RequireComponent(typeof(TwistRecognizer))]
[RequireComponent(typeof(ScreenRaycaster))]
public class GestureListener : SingletonMono<GestureListener> {

    private FingerDownDetector fingerDownDetector;
    private FingerUpDetector fingerUpDetector;
    private FingerMotionDetector fingerMotionDetector;

    private TapRecognizer[] tapRecognizers;
    private DragRecognizer dragRecognizer;
    private PinchRecognizer pinchRecognizer;
    private SwipeRecognizer swipeRecognizer;
    private LongPressRecognizer longPressRecognizer;
    private TwistRecognizer twistRecognizer;

    void Awake() {
        fingerDownDetector = GetComponent<FingerDownDetector>();
        fingerUpDetector = GetComponent<FingerUpDetector>();
        fingerMotionDetector = GetComponent<FingerMotionDetector>();

        tapRecognizers = GetComponents<TapRecognizer>();
        dragRecognizer = GetComponent<DragRecognizer>();
        pinchRecognizer = GetComponent<PinchRecognizer>();
        swipeRecognizer = GetComponent<SwipeRecognizer>();
        longPressRecognizer = GetComponent<LongPressRecognizer>();
        twistRecognizer = GetComponent<TwistRecognizer>();
    }

    // Use this for initialization
    void Start () {
        FingerGestures.GlobalTouchFilter = ignoreUI;
    }

    // Update is called once per frame
    void Update () {
	
	}

    bool ignoreUI(int fingerIndex, Vector2 position) {
#if UNITY_EDITOR
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return false;
        }
#else
            if (ClickIsOverUI.Instance.IsPointerOverUIObject(Input.GetTouch(0).position))
            {
                return false;
            }
#endif
        return true;
    }

    public void RegisterFingerDownEventHandler(FingerEventDetector<FingerDownEvent>.FingerEventHandler eventHandler) {
        fingerDownDetector.OnFingerDown += eventHandler;
    }

    public void UnRegisterFingerDownEventHandler(FingerEventDetector<FingerDownEvent>.FingerEventHandler eventHandler)
    {
        fingerDownDetector.OnFingerDown -= eventHandler;
    }

    public void RegisterFingerUpEventHandler(FingerEventDetector<FingerUpEvent>.FingerEventHandler eventHandler) {
        fingerUpDetector.OnFingerUp += eventHandler;
    }

    public void UnRegisterFingerUpEventHandler(FingerEventDetector<FingerUpEvent>.FingerEventHandler eventHandler)
    {
        fingerUpDetector.OnFingerUp -= eventHandler;
    }

    public void RegisterFingerMotionEventHandler(FingerEventDetector<FingerMotionEvent>.FingerEventHandler eventHandler)
    {
        fingerMotionDetector.OnFingerStationary += eventHandler;
    }

    public void UnRegisterFingerMotionEventHandler(FingerEventDetector<FingerMotionEvent>.FingerEventHandler eventHandler)
    {
        fingerMotionDetector.OnFingerStationary -= eventHandler;
    }

    public void RegisterGestureTapEventHandler(GestureRecognizerTS<TapGesture>.GestureEventHandler eventHandler)
    {
        foreach (var tapRecognizer in tapRecognizers)
        {
            tapRecognizer.OnGesture += eventHandler;
        }
    }

    public void UnRegisterGestureTapEventHandler(GestureRecognizerTS<TapGesture>.GestureEventHandler eventHandler)
    {
        foreach (var tapRecognizer in tapRecognizers)
        {
            tapRecognizer.OnGesture -= eventHandler;
        }
    }

    public void RegisterGestureDragEventHandler(GestureRecognizerTS<DragGesture>.GestureEventHandler eventHandler)
    {
        dragRecognizer.OnGesture += eventHandler;
    }

    public void UnRegisterGestureDragEventHandler(GestureRecognizerTS<DragGesture>.GestureEventHandler eventHandler)
    {
        dragRecognizer.OnGesture -= eventHandler;
    }

    public void RegisterGesturePinchEventHandler(GestureRecognizerTS<PinchGesture>.GestureEventHandler eventHandler)
    {
        pinchRecognizer.OnGesture += eventHandler;
    }

    public void UnRegisterGesturePinchEventHandler(GestureRecognizerTS<PinchGesture>.GestureEventHandler eventHandler)
    {
        pinchRecognizer.OnGesture -= eventHandler;
    }

    public void RegisterGestureSwipeEventHandler(GestureRecognizerTS<SwipeGesture>.GestureEventHandler eventHandler)
    {
        swipeRecognizer.OnGesture += eventHandler;
    }

    public void UnRegisterGestureSwipeEventHandler(GestureRecognizerTS<SwipeGesture>.GestureEventHandler eventHandler)
    {
        swipeRecognizer.OnGesture -= eventHandler;
    }

    public void RegisterGestureTwistEventHandler(GestureRecognizerTS<TwistGesture>.GestureEventHandler eventHandler)
    {
        twistRecognizer.OnGesture += eventHandler;
    }

    public void UnRegisterGestureTwistEventHandler(GestureRecognizerTS<TwistGesture>.GestureEventHandler eventHandler)
    {
        twistRecognizer.OnGesture -= eventHandler;
    }

    public void RegisterGestureLongPressEventHandler(GestureRecognizerTS<LongPressGesture>.GestureEventHandler eventHandler)
    {
        longPressRecognizer.OnGesture += eventHandler;
    }

    public void UnRegisterGestureLongPressEventHandler(GestureRecognizerTS<LongPressGesture>.GestureEventHandler eventHandler)
    {
        longPressRecognizer.OnGesture -= eventHandler;
    }
}
